package com.iims.flowable.flow;

import com.iims.common.exception.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author fanpei
 * @date 2021/6/24 19:23
 */
public class CommonUtil {
    private static final Pattern MOBILE_PATTERN = Pattern.compile("^[1][0-9]{10}$");
    private static final Pattern PHONE_WITH_AREA_CODE_PATTERN = Pattern.compile("^[0][1-9]{2,3}-?[0-9]{5,10}$");
    private static final Pattern PHONE_WITHOUT_AREA_CODE_PATTERN = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");
    private static final String ZERO = "0";

    public CommonUtil() {
    }

    public static String trimToEmptyStr(String str) {
        return StringUtils.trimToEmpty(str);
    }

    public static boolean isEmptyObject(Object object) {
        return ObjectUtils.isEmpty(object);
    }

    public static void isEmptyObject(Object object, String message) {
        if (isEmptyObject(object)) {
            throw new BaseException(message);
        }
    }

    public static boolean isNotEmptyObject(Object object) {
        return !ObjectUtils.isEmpty(object);
    }

    public static boolean isEmptyStr(String str) {
        return StringUtils.isEmpty(str);
    }

    public static void isEmptyStr(String str, String message) {
        if (isEmptyStr(str)) {
            throw new BaseException(message);
        }
    }

    public static void isEmptyMapWithKey(Map<String, Object> map, String key, String message) {
        if (map == null || isEmptyObject(map.get(key))) {
            throw new BaseException(message);
        }
    }

    public static boolean isNotEmptyStr(String str) {
        return !StringUtils.isEmpty(str);
    }

    public static boolean isEmptyAfterTrim(String str) {
        return StringUtils.isEmpty(StringUtils.trimToEmpty(str));
    }

    public static void isEmptyAfterTrim(String str, String message) {
        if (isEmptyAfterTrim(str)) {
            throw new BaseException(message);
        }
    }

    public static boolean isNotEmptyAfterTrim(String str) {
        return !isEmptyAfterTrim(str);
    }

    public static <T> T isEmptyDefault(T source, T df) {
        return isNotEmptyObject(source) ? source : df;
    }

    public static boolean isExist(String str, String substr, String sepatator) {
        if (str != null && str.trim().length() != 0) {
            if (substr != null && substr.trim().length() != 0) {
                String[] strArr = str.split(sepatator);
                int size = strArr.length;

                for (int i = 0; i < size; ++i) {
                    if (strArr[i].equals(substr)) {
                        return true;
                    }
                }

                return false;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static String[] getIds(String srcIds, String splitChar) {
        String[] ids = null;
        boolean spBegin = false;
        if (srcIds != null && srcIds.length() != 0) {
            if (srcIds.indexOf(splitChar) == 0) {
                spBegin = true;
            }

            String[] strSplitIds = srcIds.trim().split(splitChar);
            int len = strSplitIds.length;
            int i;
            if (spBegin) {
                if (len <= 1) {
                    return null;
                }

                ids = new String[len - 1];

                for (i = 1; i < len; ++i) {
                    ids[i - 1] = strSplitIds[i];
                }
            } else {
                ids = new String[len];

                for (i = 0; i < len; ++i) {
                    ids[i] = strSplitIds[i];
                }
            }

            return ids;
        } else {
            return null;
        }
    }

    public static String toUtf8String(String s) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (c >= 0 && c <= 255) {
                sb.append(c);
            } else {
                byte[] b;
                try {
                    b = Character.toString(c).getBytes("utf-8");
                } catch (Exception var7) {
                    b = new byte[0];
                }

                for (int j = 0; j < b.length; ++j) {
                    int k = b[j];
                    if (k < 0) {
                        k += 256;
                    }

                    sb.append("%" + Integer.toHexString(k).toUpperCase());
                }
            }
        }

        return sb.toString();
    }

    public static String castStringBySeparator(String source, String separator) {
        source = source.replace(separator, "'" + separator + "'");
        source = "'" + source + "'";
        return source;
    }

    public static boolean isMobile(String str) {
        boolean b = false;
        Matcher m = MOBILE_PATTERN.matcher(str);
        b = m.matches();
        return b;
    }

    public static boolean isPhone(String str) {
        Matcher m = null;
        boolean b = false;
        if (str.startsWith("0")) {
            m = PHONE_WITH_AREA_CODE_PATTERN.matcher(str);
            b = m.matches();
        } else {
            m = PHONE_WITHOUT_AREA_CODE_PATTERN.matcher(str);
            b = m.matches();
        }

        return b;
    }

    public static String getUuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String getUuid2() {
        return UUID.randomUUID().toString();
    }

    public static String addLike(String str) {
        return "'%" + str + "%'";
    }

    public static String addLikeLeft(String str) {
        return "'%" + str + "'";
    }

    public static String addLikeRight(String str) {
        return "'" + str + "%'";
    }

    public static String cutString(String str, int num, String charsetName) {
        try {
            int length = str.getBytes(charsetName).length;
            if (length > num) {
                str = str.substring(0, str.length() - 1);
                str = cutString(str, num, charsetName);
            }

            return str;
        } catch (UnsupportedEncodingException var4) {
            var4.printStackTrace();
            return null;
        }
    }
}
